package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listerner.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-08
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {

        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,
                            new SubjectExcelListener(subjectService))
                    .sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public List<OneSubject> getAllSubject() {

        //获取课程一级列表
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();

        wrapperOne.eq("parent_id", "0");
        List<EduSubject> OnesubjectList = baseMapper.selectList(wrapperOne);

        //获取二级分类列表
        QueryWrapper<EduSubject> twoWrapper = new QueryWrapper<>();

        twoWrapper.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(twoWrapper);

        //组合一级分类
        ArrayList<OneSubject> finalSubjects = new ArrayList<>();


        for (int i = 0; i < OnesubjectList.size(); i++) {
            //获取一级分类列表中的EduSubject
            EduSubject eduSubject = OnesubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
            //复制eduSubject 到oneSubject
            BeanUtils.copyProperties(eduSubject, oneSubject);
            //添加到一级分类列表中
            finalSubjects.add(oneSubject);
            //存储二级分类
            ArrayList<TwoSubject> twoSubjects = new ArrayList<>();

            for (int j = 0; j < twoSubjectList.size(); j++) {
                //获取二级级分类列表中的EduSubject
                EduSubject eduSubject2 = twoSubjectList.get(j);
                //判断二级分类parentid和一级分类id是否一样
                if (eduSubject2.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    //把tSubject值复制到TwoSubject里面，放到twoFinalSubjectList里面
                    BeanUtils.copyProperties(eduSubject2, twoSubject);
                    twoSubjects.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoSubjects);

        }


        return finalSubjects;
    }
}
